<template>
	<form class="form-horizontal">
		<div class="form-group">
			<label for="inputEmail3" class="col-sm-2 control-label">账号</label>
			<div class="col-sm-10">
				<input type="text" class="form-control" id="count" v-model="loginData.username" placeholder="请输入您的账号">
			</div>
		</div>
		<div class="form-group">
			<label for="inputPassword3" class="col-sm-2 control-label">密码</label>
			<div class="col-sm-10">
				<input type="password" class="form-control" id="pass" v-model="loginData.password" placeholder="请输入您的密码">
			</div>
		</div>
		<div class="form-group">
			<div class="col-sm-offset-2 col-sm-10">
				<div class="checkbox">
					<label>
						<input type="checkbox"> 记住密码
					</label>
				</div>
			</div>
		</div>
		<div class="form-group">
			<div class="col-sm-offset-2 col-sm-10">
				<button type="submit" class="btn btn-default" @click="login">登录</button>
			</div>
		</div>
	</form>
</template>

<script>
	import Vue from 'vue'

	export default{
		name:'login',
    data:()=>{
      return {
        loginData:{
          username:"12345678",
          password:"12345678"
        }
      }
    },
    methods:{
      login:function(){
          if(!this.loginData.username){
            alert("用户名不能为空");
            return;
          }
          if(!this.loginData.password){
            alert("输入你的密码");
            return;
          }
        // console.info(this.loginData)
        // http://localhost:8083/login?username='+loginData.username+'&password='+loginData.password+'
        Vue.axios.get('http://localhost:8083/login?username='+this.loginData.username+'&password='+this.loginData.password)
        .then(res=>{
          console.log(res);
          if(res.data.ok){
            window.sessionStorage.setItem('user',res.data.user.username);
            window.sessionStorage.setItem('role',res.data.user.role);
            if( res.data.user.role == 1){ //登录成功
            console.log('我是超级管理员');
            this.$router.push("/"); //页面跳转
            // console.info(res)
             }else if(res.data.user.role == 2){
               console.log('我是老师');
              this.$router.push("/");//页面跳转
             }else if(res.data.user.role == 3){
               console.log('我是学生');
              this.$router.push("/examList");//页面跳转
             }
          }else{           //登录失败
             alert(res.data.info)
           }
        })
        .catch(err=>{
          console.log(err);
        })
      }
    }
	}
</script>

<style scoped="scoped">


</style>
